<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <link href="../css/theme.css" rel="stylesheet" type="text/css">
  <link type="text/css" rel="stylesheet" href="../css/print.css" media="print">
  <title>问卷填写界面</title>
  <script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
  <script type="text/javascript" src="../js/qm.core.js"></script>
  <script type="text/javascript" src="../js/qm.answer.js"></script>
  <link href="../js/toastr/toastr.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../js/toastr/toastr.min.js"></script>
  <script type="text/javascript" src="../js/common.js"></script>
  <script>
    document.write('<script src="../js/util/require.config.js?v='+new Date().getTime()+'"><\/script>')
  </script>
  <script type="text/javascript">
    var contextPath = '/qms';
    function getQueryVariable(variable){
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
          var pair = vars[i].split("=");
          if (pair[0] == variable) {
              return pair[1];
          }
      }
      return (undefined);
    }
    $(document).ready(function () {
      //计算div#qmTianxie的高度
      $("#qmTianxie").height(window.innerHeight - 65);

      $("#back").click(function () {
        // if ($("#_back_url_homepage").val() != "" && $("#_back_url_homepage").val() == "HOMEPAGE") {
        //   window.location.href = "./home.html";
        // }else{
        //   window.location.href = "./list.html";
        // } 
        window.history.go(-1)
      });
      var id = getQueryVariable("id")
      $.ajax({
        url: obpmFilePath + '/magic-api/qms/questionnaires/'+ id,
        async: false,
        type: "GET",
        success: function (response) {
          if(response.errcode == 0){
            $(".details").find("#question").val(response.data.content);
            $(".details").find("#title").html(response.data.title)
            $(".details").find("#questionnaire_id").val(response.data.id)
            QM.answer.init($("#question").val(), $("#answer").val());
          }
        }
      });
    });
    function doSubmit() {
      if (confirm("是否提交案卷(提交后不可以再作答)?")) {
        var flag = QM.answer.checkRequired();
        if (flag) {
          var json = QM.answer.encodeJson();
          var $content = $("#answer");
          $content.val(json);
          var dataJson = {
            content: $("#question").val(),
            answer: json,
            questionnaire_id: $("#questionnaire_id").val()
          }
          $.ajax({
            url: obpmFilePath + '/magic-api/qms/answers/submit',
            async: false,
            type: "POST",
            dataType: 'json',
            data: JSON.stringify(dataJson),
            contentType: 'application/json',
            success: function (response) {
              if(response.errcode == 0){
                window.location.href = './save.html?id=' + $("#questionnaire_id").val() + "&urlType=" + getQueryVariable("urlType");
              }
            }
          });
        } else {
          //alert("请填写带 * 必填题");
          Common.Util.showMessage("请填写带 * 必填题", "warning");
        }
      }
    }

  </script>
</head>

<body style="text-align: center;" class="printOver">
  <form id="formList" class="details" name="formList" action="save.action" method="post">
    <input type='hidden' name='content.id' value='' />
    <input type="hidden" name="s_jumpType" value='HOMEPAGE' id="formList_s_jumpType">
    <input type='hidden' id="questionnaire_id" name='content.questionnaire_id' value='' />
    <textarea id="question" name='content.content'
      style='display: none'></textarea>
    <textarea id="answer" name='content.answer' style='display: none'></textarea>
    <input type="hidden" id="_back_url_type" name="type" value="" />
    <input type="hidden" id="_back_url_status" name="status" value="" />
    <input type="hidden" id="_back_url_currpage" name="_currpage" value="" />
    <input type="hidden" id="_back_url_pagelines" name="_pagelines" value="" />

    <div style="text-align:left; background:#f5f5f5; height: 65px;" class="noprint">
      <a class="btn btn-primary" id="back" href='javascript:void(0)'>返回</a>
      <a class="btn btn-primary_green" href='javascript:doSubmit();'>提交</a>
      <div class="right-btn">
        <a class='btn btn-primary_Blue btn-preview' href='javascript:print();'>打印</a>
      </div>
    </div>
    <div id="qmTianxie">
      <div style="padding-bottom: 10px; border-bottom: 3px #8ad748 solid;">
        <h1 id="title"></h1>
      </div>
      <div>
        <div id="maiDiv" style="text-align:left;"></div>
      </div>
    </div>
  </form>

</body>

</html>